package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dao.LessonMapper;
import com.zhihuishu.micro.course.dto.LessonDto;
import com.zhihuishu.micro.course.model.Chapter;
import com.zhihuishu.micro.course.model.Lesson;
import com.zhihuishu.micro.course.service.LessonTreenityDBService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service("lessonAllServiceDB")
public class LessonAllServiceDBImpl extends BaseTreenityBatchServiceDBImpl<LessonDto, Lesson> implements LessonTreenityDBService {

    @Resource
    private LessonMapper lessonMapper;


    @Override
    public LessonDto detail(Integer lessonId) {
        Lesson chapter = lessonMapper.get(lessonId);
        LessonDto chapterDto = toDto(chapter);
        return chapterDto;
    }

    @Override
    public void update(LessonDto chapterDto, long updateUserId) {
        Lesson model = toModel(chapterDto);
        lessonMapper.update(model);
    }

    @Override
    public void overwriteVideoInfo(LessonDto dto, long updateUserId){
        Lesson model = toModel(dto);
        lessonMapper.overwriteVideoInfo(model);
    }


    @Override
    public void delete(Integer lessonId, long deleteUserId) {
        lessonMapper.remove(lessonId);
    }

    @Override
    public void sort(List<LessonDto> dbDtoList, long updateUserId) {
        List<Lesson> modelList = new LinkedList<Lesson>();
        for (LessonDto chapterDto : dbDtoList) {
            modelList.add(toModel(chapterDto));
        }
        lessonMapper.sort(modelList);
    }

    @Override
    public Integer create(LessonDto chapter, long creator) {
        Lesson model = toModel(chapter);

        lessonMapper.insertSelective(model);

        Integer lessonId = model != null ? model.getId() : null;
        chapter.setId(lessonId);

        return lessonId;
    }

    public LessonDto toDto(Lesson model) {
        if (model == null) return null;
        LessonDto dto = new LessonDto();
        dto.setCourseId(Long.valueOf(model.getCourseId()));
        dto.setChapterId(model.getChapterId());
        dto.setId(model.getId());
        dto.setIntroduction(model.getIntroduction());
        dto.setName(model.getName());
        dto.setRank(model.getOrderNumber());
        //dto.setPlanDay(model.getPlanDay());
        dto.setVideoId(model.getVideoId());
        dto.setVideoSec(model.getVideoSec());
        dto.setVideoImg(model.getVideoImg());
        dto.setVideoUrl(model.getVideoUrl());
        if (model.getIsHaveChildrenLesson() != null) {
            dto.setHaveChildren(model.getIsHaveChildrenLesson());
        }
        return dto;
    }

    public Lesson toModel(LessonDto dto) {
        if (dto == null) return null;
        Lesson model = new Lesson();
        if (dto.getCourseId() != null) {
            model.setCourseId(dto.getCourseId());
        }
        model.setChapterId(dto.getChapterId());
        model.setId(dto.getId());
        model.setIntroduction(dto.getIntroduction());
        model.setName(dto.getName());
        model.setOrderNumber(dto.getRank());
       // model.setPlanDay(dto.getPlanDay());
        model.setVideoId(dto.getVideoId());
        model.setIsHaveChildrenLesson(dto.getHaveChildren());
        model.setVideoSec(dto.getVideoSec());
        model.setVideoImg(dto.getVideoImg());
        model.setVideoUrl(dto.getVideoUrl());
        return model;
    }



    @Override
    public List<Integer> listDtoIds(long courseId) {
        List<Integer> lessonIds = lessonMapper.listByCourseId((int) courseId);
        return lessonIds;
    }

    @Override
    public List<LessonDto> list(List<Integer> listLessonIds) {
        final List<LessonDto> chapters = new ArrayList<LessonDto>();

        if (!CollectionUtils.isEmpty(listLessonIds)) {
            List<Lesson> list = lessonMapper.getList(listLessonIds);
            for (Lesson lesson : list) {
                chapters.add(toDto(lesson));
            }
        }
        return chapters;
    }

    @Override
    public void deleteMulti(List<Integer> dtoIds, long deleteUserId) {

    }
}
